package com.mediacloud.app.newsmodule.activity;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.eventTrack.CDPTrackReportManager;
import com.eventTrack.TXReportManager;
import com.mediacloud.app.assembly.util.FunKt;
import com.mediacloud.app.assembly.util.StringUtils;
import com.mediacloud.app.assembly.util.Utility;
import com.mediacloud.app.assembly.views.BadgeView;
import com.mediacloud.app.assembly.views.LikeBadgeView;
import com.mediacloud.app.collection.CollectEvent;
import com.mediacloud.app.model.interfaces.IPermissionsAgree;
import com.mediacloud.app.model.interfaces.PermissionUtil;
import com.mediacloud.app.model.news.ArticleItem;
import com.mediacloud.app.model.news.CatalogItem;
import com.mediacloud.app.newsmodule.R;
import com.mediacloud.app.newsmodule.adaptor.comment.x.XCommentActivity;
import com.mediacloud.app.newsmodule.adaptor.comment.x.XCommentDialogFragment;
import com.mediacloud.app.newsmodule.addnewslike.p.NewsLikePresenter;
import com.mediacloud.app.newsmodule.fragment.baoliao.activity.BaoLiaoDetailActivity;
import com.mediacloud.app.newsmodule.utils.CollectionController;
import com.mediacloud.app.newsmodule.utils.LoginUtils;
import com.mediacloud.app.newsmodule.utils.ToastUtil;
import com.mediacloud.app.newsmodule.utils.TypeToolKt;
import com.mediacloud.app.newsmodule.utils.share.ShareWrap;
import com.mediacloud.app.newsmodule.utils.support.SupportKit;
import com.mediacloud.app.newsmodule.view.CommentDialog;
import com.mediacloud.app.reslib.config.NewsDetailStyleConfig;
import com.mediacloud.app.reslib.enums.AppFactoryGlobalConfig;
import com.mediacloud.app.user.model.UserInfo;
import com.utils.RxUtils;
import com.utils.SystemServiceManager;


import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;


public abstract class StyleSetActivity extends HandleDataCollectActivity implements CollectionController.CollectStatusListener {
    protected boolean titleBarIsOpen = true;
    protected AppFactoryGlobalConfig.ServerAppConfigInfo serverAppConfigInfo;
    //顶部那一排按钮的根布局
    protected ViewGroup bottomBarLayout;
    //底部的返回按钮
    protected View bottomBack;
    //评论框
    protected TextView tvLetmeSay;
    protected View tv_letme_say_gp;
    protected View linear_comment_layout;
    //评论框表情
    protected ImageView ivFace;
    //收藏
    protected ImageView bottomCollectionBtn;
    //收藏布局
    protected LinearLayout linearCollectLayout;
    //分享布局
    protected LinearLayout linearShareLayout;

    //点赞
    protected LikeBadgeView likeBadgeView;
    //评论图标
    protected BadgeView commentBadgeView;

    //分享
    protected ImageView bottomShareView;


    public NewsDetailStyleConfig newsDetailStyleConfig;
    //点赞相关
    public NewsLikePresenter newsLikePresenter;
    //收藏相关
    protected CollectionController collectionController;
    //导航相关
    public CatalogItem catalogItem;
    //分享相关
    public ShareWrap shareWrap;
    private boolean isTencentAct = false;
    public boolean isVrUrl = false;
    private NetChangeReceiver receiver;
    public boolean isLive;
    public View.OnClickListener likeClickListener;
    public ImageView ivBottomHead;
    private Utility utility;
    public String isPreview = "";//是否是预览稿件
    public String isComment = "1";//默认可以评论

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        serverAppConfigInfo = AppFactoryGlobalConfig.getAppServerConfigInfo(this);
        newsDetailStyleConfig = serverAppConfigInfo.getNewsDetailConfig();
        if (openTitleBarSetting()) {
            if (newsDetailStyleConfig == null) {
                newsDetailStyleConfig = new NewsDetailStyleConfig();
                newsDetailStyleConfig.initDefault();
            }
            toggleTitleBar(newsDetailStyleConfig.isShowTopBar());
        }
        if (getIntent() != null) {
            isTencentAct = getIntent().getBooleanExtra("isTencentAct", false);
        }

        //ios闪视频样式没有海报分享
//        shareWrap = new ShareWrap(this, this instanceof ImageTextDetailActivity2 /*|| this instanceof ShanShiPinVideoVodHorizontalStyleActivity*/,false);
        shareWrap = new ShareWrap(this, false);
        shareWrap.setShowFunction(false);
        shareWrap.setTencentAct(isTencentAct);
        shareWrap.setVrUrl(isVrUrl);
        //   shareWrap.removeGridItem(SBShareUtils.CopyLink);
        setWhiteBottomButton();
        initBottomView();
        initNetStatusBroad();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    /**
     * @return 是否打开顶栏设置，默认情况下是关闭的
     */
    protected boolean openTitleBarSetting() {
        return false;
    }

    protected void toggleTitleBar(boolean open) {
        if (open == titleBarIsOpen) {
            return;
        }
        titleBarIsOpen = open;
        if (textSizeCtl != null)
            textSizeCtl.setVisibility(View.GONE);
        showView(open, mTitileBar);
    }

    /**
     * 如果直接放在layout的布局中
     */
    protected void initBottomView() {
        if (bottomBarLayout == null) {
            bottomBarLayout = findViewById(R.id.linear_news_bottom_back_bar);
        }
        LinkedHashSet<String> showContent = newsDetailStyleConfig.getLinkedHashSet();
        if (showContent.size() == 0 && titleBarIsOpen) {
            bottomBarLayout.setVisibility(View.GONE);
            Log.w(TAG, "initBottomView bottom content is empty and back show on the top");
        }
        initBottomBack();
        initBottomHead();
        initCollect();
        initShare();
        initCommentButton();
        initLikeButton();
        initLetmeSay();
        orderView();


    }

    /**
     * 对底部的View进行重新布局
     */
    protected void layoutBottomView(boolean laymeSayShow) {
        if (laymeSayShow) {//显示评论框,几个按钮以wrap_content占据整个布局
            LinearLayout linearLayout = findViewById(R.id.linear_comment_layout);
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) linearLayout.getLayoutParams();
            params.width = ViewGroup.LayoutParams.WRAP_CONTENT;
            params.weight = 1f;
            linearLayout.setLayoutParams(params);

            linearLayout = findViewById(R.id.linear_like_layout);
            params = (LinearLayout.LayoutParams) linearLayout.getLayoutParams();
            params.width = ViewGroup.LayoutParams.WRAP_CONTENT;
            params.weight = 1f;
            linearLayout.setLayoutParams(params);

            linearLayout = findViewById(R.id.linear_collect_layout);
            params = (LinearLayout.LayoutParams) linearLayout.getLayoutParams();
            params.width = ViewGroup.LayoutParams.WRAP_CONTENT;
            params.weight = 1f;
            params.gravity =Gravity.CENTER;
            linearLayout.setLayoutParams(params);

            linearLayout = findViewById(R.id.linear_share_layout);
            params = (LinearLayout.LayoutParams) linearLayout.getLayoutParams();
            params.width = ViewGroup.LayoutParams.WRAP_CONTENT;
            params.weight = 1f;
            params.gravity =Gravity.CENTER;
            linearLayout.setLayoutParams(params);
        } else {
            LinearLayout linearLayout = findViewById(R.id.linear_comment_layout);
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) linearLayout.getLayoutParams();
            params.width = 0;
            params.weight = 1;
            linearLayout.setLayoutParams(params);

            linearLayout = findViewById(R.id.linear_like_layout);
            params = (LinearLayout.LayoutParams) linearLayout.getLayoutParams();
            params.width = 0;
            params.weight = 1;
            linearLayout.setLayoutParams(params);

            linearLayout = findViewById(R.id.linear_collect_layout);
            params = (LinearLayout.LayoutParams) linearLayout.getLayoutParams();
            params.width = 0;
            params.weight = 1;
            linearLayout.setLayoutParams(params);

            linearLayout = findViewById(R.id.linear_share_layout);
            params = (LinearLayout.LayoutParams) linearLayout.getLayoutParams();
            params.width = 0;
            params.weight = 1;
            linearLayout.setLayoutParams(params);
        }
    }

    /**
     * 对底部的View进行排序，根据后台的下发顺序显示
     */
    protected void orderView() {
        HashMap<String, View> viewMap = prepareOriginalViewMap();
        LinkedHashSet<String> linkedHashSet = newsDetailStyleConfig.getLinkedHashSet();
//        if (isComment.equals("0")) {
//            viewMap.remove(NewsDetailStyleConfig.COMMENT_BUTTON);
//            viewMap.remove(NewsDetailStyleConfig.COMMENT_BOX);
//        }
        /**
         * 判断是否配置了点赞或者评论的按钮，配置的话统一靠左边展示 否则收藏和分享还是居中展示
         */
        if (viewMap.containsKey(NewsDetailStyleConfig.COMMENT_BUTTON)) {
            if (linearCollectLayout != null)
                linearCollectLayout.setGravity(Gravity.CENTER);
            if (linearShareLayout != null)
                linearShareLayout.setGravity(Gravity.CENTER);
            if (likeBadgeView != null)
                likeBadgeView.showBadgeLeft(true);
            if (commentBadgeView != null)
                commentBadgeView.showBadgeLeft(true);
        } else {
            if (linearCollectLayout != null)
                linearCollectLayout.setGravity(Gravity.CENTER);
            if (linearShareLayout != null)
                linearShareLayout.setGravity(Gravity.CENTER);
            if (likeBadgeView != null)
                likeBadgeView.showBadgeLeft(false);
            if (commentBadgeView != null)
                commentBadgeView.showBadgeLeft(false);
        }
        bottomBarLayout.removeAllViews();
        bottomBarLayout.addView(bottomBack);
        bottomBarLayout.addView(ivBottomHead);
        for (String item : linkedHashSet) {
            View view = viewMap.remove(item);
            if (view != null) {
                bottomBarLayout.addView(view);
            }
        }

        //本身如果 linkedHashSet 没有对应的值的话,说明后台配置显示可以不用处理，但是有可能会有一些其他的调用处理因此还是添加回去
        if (!viewMap.isEmpty()) {
            Set<Map.Entry<String, View>> entrySet = viewMap.entrySet();
            for (Map.Entry<String, View> entry : entrySet) {
                bottomBarLayout.addView(entry.getValue());
            }
        }
        if (linkedHashSet.isEmpty() || viewMap.isEmpty()) {//如果没有配置按钮去掉pading 等于隐藏了底部view
            bottomBarLayout.setPadding(0, 0, 0, 0);
        }
    }

    protected HashMap<String, View> prepareOriginalViewMap() {
        HashMap<String, View> viewMap = new HashMap<>();
        viewMap.put(NewsDetailStyleConfig.COLLECT_BUTTON, findViewById(R.id.linear_collect_layout));
        viewMap.put(NewsDetailStyleConfig.SHARE_BUTTON, findViewById(R.id.linear_share_layout));
        viewMap.put(NewsDetailStyleConfig.LIKE_BUTTON, findViewById(R.id.linear_like_layout));
        viewMap.put(NewsDetailStyleConfig.COMMENT_BUTTON, findViewById(R.id.linear_comment_layout));
        viewMap.put(NewsDetailStyleConfig.COMMENT_BOX, findViewById(R.id.tv_letme_say_gp));
        return viewMap;
    }

    protected void initLikeButton() {
        likeBadgeView = bottomBarLayout.findViewById(R.id.likeBadgeView);
        likeBadgeView.setTintIcon(isWhiteColor);
        likeBadgeView.setResDiss();
        //用于点赞按钮的点击事件采集
        likeBadgeView.setmButtonClickLike(this);
        boolean openLikeBtn = articleItem.getTagsflag() == 1 && serverAppConfigInfo.getOtherFunction() != null && serverAppConfigInfo.getOtherFunction().getSupport() > 0;
        showLikeButtonWithConfig(openLikeBtn);
    }

    protected boolean isCommentDialog = false;

    protected void initCommentButton() {
        commentBadgeView = bottomBarLayout.findViewById(R.id.commentBadgeView);
        RxUtils.rxClick(commentBadgeView, new RxUtils.OnClickInterf() {
            @Override
            public void onViewClick(View view) {
                TXReportManager.getInstance().app_jhapp_content_detail_interact("评论",
                        null,
                        null, articleItem);
                if (!TextUtils.isEmpty(isPreview) && isPreview.equals("1")) {
                    ToastUtil.show(StyleSetActivity.this, "预览稿件不支持当前操作");
                    return;
                }
                if (isCommentDialog) {
                    XCommentDialogFragment fragment = new XCommentDialogFragment();
                    Bundle bundle = new Bundle();

                    bundle.putParcelable("data", articleItem);
                    bundle.putBoolean("HIDE_COMMENT_ICO", true);
                    bundle.putInt("type", 1);
                    fragment.setArguments(bundle);
                    fragment.show(getSupportFragmentManager(), "bottom");
                } else {
                    Intent intent = new Intent();
                    articleItem.setContent(null);
                    intent.putExtra("data", articleItem);
                    if (StyleSetActivity.this instanceof BaoLiaoDetailActivity) {
                        intent.putExtra("type", 5);
                    }
                    intent.setClass(StyleSetActivity.this, XCommentActivity.class);
                    startActivity(intent);
                }
            }
        });

        showCommentButtonWithConfig(true);
    }

    private void initShare() {
        bottomShareView = bottomBarLayout.findViewById(R.id.image_bottom_share);
        bottomShareView.setOnClickListener(v -> {
            if (!TextUtils.isEmpty(isPreview) && isPreview.equals("1")) {
                ToastUtil.show(this, "预览稿件不支持当前操作");
                return;
            }
            shareWrap.show(mRootView);
        });
        showShareWithConfig(true);
    }

    private void initCollect() {
        bottomCollectionBtn = bottomBarLayout.findViewById(R.id.image_bottom_collect);
        bottomCollectionBtn.setOnClickListener(v -> {
            if (!TextUtils.isEmpty(isPreview) && isPreview.equals("1")) {
                ToastUtil.show(this, "预览稿件不支持当前操作");
                return;
            }
            bottomCollectionBtn.setEnabled(true);
            collectionController.collection();
        });
        showCollectWithConfig(true);
    }

    private void initBottomBack() {
        bottomBack = bottomBarLayout.findViewById(R.id.view_bottom_back);
        bottomBack.setOnClickListener(v -> finish());
        showBottomBack(!titleBarIsOpen);
    }

    private void initBottomHead() {
        ivBottomHead = bottomBarLayout.findViewById(R.id.ivBottomHead);
        ivBottomHead.setVisibility(View.GONE);
        if (UserInfo.isLogin(this)) { //固定展示用户头像，没登录用app图标
            FunKt.load(ivBottomHead,
                    UserInfo.getUserInfo(this).getAvatar(),
                    ContextCompat.getDrawable(this, R.drawable.default_head));
        } else {
            ivBottomHead.setImageResource(R.drawable.ic_launcher);
        }

    }

    protected boolean commentIsOpen = false;

    protected void initLetmeSay() {
        tv_letme_say_gp = bottomBarLayout.findViewById(R.id.tv_letme_say_gp);
        linear_comment_layout = bottomBarLayout.findViewById(R.id.linear_comment_layout);
        tvLetmeSay = bottomBarLayout.findViewById(R.id.tv_letme_say);
        linearCollectLayout = bottomBarLayout.findViewById(R.id.linear_collect_layout);
        linearShareLayout = bottomBarLayout.findViewById(R.id.linear_share_layout);
        ivFace = bottomBarLayout.findViewById(R.id.ivFace);
        tvLetmeSay.setOnClickListener(v -> {
            if (!TextUtils.isEmpty(isPreview) && isPreview.equals("1")) {
                ToastUtil.show(this, "预览稿件不支持当前操作");
                return;
            }
            if (!UserInfo.isLogin(this)) {
                LoginUtils.invokeLogin(this, 2);
                return;
            }
            if (this instanceof BaoLiaoDetailActivity) {
                commentIsOpen = true;
                CommentDialog.Companion.startActivity(this, articleItem, false, 5);

            } else {
                commentIsOpen = true;
                CommentDialog.Companion.startActivity(this, articleItem, false, TypeToolKt.getCommentTool(articleItem.getType()));
            }
        });
        ivFace.setOnClickListener(v -> {
            if (!TextUtils.isEmpty(isPreview) && isPreview.equals("1")) {
                ToastUtil.show(this, "预览稿件不支持当前操作");
                return;
            }
            if (!UserInfo.isLogin(this)) {
                LoginUtils.invokeLogin(this, 2);
                return;
            }
            if (this instanceof BaoLiaoDetailActivity) {
                commentIsOpen = true;
                CommentDialog.Companion.startActivity(this, articleItem, true, 5);

            } else {
                commentIsOpen = true;
                CommentDialog.Companion.startActivity(this, articleItem, true, TypeToolKt.getCommentTool(articleItem.getType()));
            }
        });
        showLetMeSayWithConfig(true);
    }


    /**
     * 显示底部返回
     *
     * @param show true 显示， false 隐藏
     */
    protected void showBottomBack(boolean show) {
        showView(show, bottomBack);
    }

    protected void showLetMeSayWithConfig(boolean isComment) {
        boolean show = newsDetailStyleConfig.getLinkedHashSet().contains(NewsDetailStyleConfig.COMMENT_BOX);
        showView(show, tv_letme_say_gp);
        showView(show, tvLetmeSay);
        if (isComment) {
            if (tvLetmeSay != null) {
                tvLetmeSay.setClickable(true);
                tvLetmeSay.setText("我来说几句");
            }
            if (ivFace != null) {
                ivFace.setClickable(true);
            }
        } else {
            if (tvLetmeSay != null) {
                tvLetmeSay.setClickable(false);
                tvLetmeSay.setText("发布者已关闭评论");
            }
            if (ivFace != null) {
                ivFace.setClickable(false);
            }
        }
        // layoutBottomView(show);
    }

    protected void showCollectWithConfig(boolean show) {
        show = newsDetailStyleConfig.getLinkedHashSet().contains(NewsDetailStyleConfig.COLLECT_BUTTON) && show;
        showView(show, bottomBarLayout.findViewById(R.id.linear_collect_layout));
    }

    protected void showShareWithConfig(boolean show) {
        Drawable defaultDrawable;
        show = newsDetailStyleConfig.getLinkedHashSet().contains(NewsDetailStyleConfig.SHARE_BUTTON) && show;
        showView(show, bottomBarLayout.findViewById(R.id.linear_share_layout));
        if (isWhiteColor) {
//            defaultDrawable = Utility.tintDrawable(Color.parseColor("#FFD8D8D8"), ContextCompat.getDrawable(this, R.drawable.common_share));
            defaultDrawable = ContextCompat.getDrawable(this, R.drawable.icon_sc);
        } else defaultDrawable = ContextCompat.getDrawable(this, R.drawable.common_share);
        if (TextUtils.isEmpty(newsDetailStyleConfig.getShowIcon().share_button_un_icon)) {
            bottomShareView.setImageDrawable(defaultDrawable);
        } else {
            //使用服务器返回的分享按钮
            FunKt.otherLoad(this, newsDetailStyleConfig.getShowIcon().share_button_un_icon, new RequestListener<Drawable>() {

                @Override
                public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                    bottomShareView.setImageDrawable(defaultDrawable);
                    if (isFinishing() || isDestroyed()) {
                        return false;
                    }
                    return false;
                }

                @Override
                public boolean onResourceReady(Drawable drawable, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                    if (isFinishing() || isDestroyed()) {
                        return false;
                    }
                    if (isWhiteColor) {
                        bottomShareView.setImageDrawable(defaultDrawable);
                    } else {
                        bottomShareView.setImageDrawable(drawable);
                    }
                    return false;
                }
            });
        }

    }

    protected void showCommentButtonWithConfig(boolean isComment) {
        Drawable defaultDrawable;
        boolean show = newsDetailStyleConfig.getLinkedHashSet().contains(NewsDetailStyleConfig.COMMENT_BUTTON);
        showView(show, bottomBarLayout.findViewById(R.id.linear_comment_layout));
        if (isWhiteColor) {
//            defaultDrawable = Utility.tintDrawable(Color.parseColor("#FFD8D8D8"), ContextCompat.getDrawable(this, R.drawable.common_comments));
            defaultDrawable = ContextCompat.getDrawable(this, R.drawable.common_comments_white);
        } else defaultDrawable = ContextCompat.getDrawable(this, R.drawable.common_comments_white);
        if (TextUtils.isEmpty(newsDetailStyleConfig.getShowIcon().comment_button_un_icon)) {
            commentBadgeView.setsetBadgeImageDrawable(defaultDrawable);
        } else {
            FunKt.otherLoad(this, newsDetailStyleConfig.getShowIcon().comment_button_un_icon, new RequestListener<Drawable>() {

                @Override
                public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                    if (isFinishing() || isDestroyed()) {
                        return false;
                    }
                    commentBadgeView.setsetBadgeImageDrawable(defaultDrawable);
                    return false;
                }

                @Override
                public boolean onResourceReady(Drawable drawable, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                    if (isFinishing() || isDestroyed()) {
                        return false;
                    }
                    if (isWhiteColor) {
                        commentBadgeView.setsetBadgeImageDrawable(defaultDrawable);
                    } else {
                        commentBadgeView.setsetBadgeImageDrawable(drawable);
                    }
                    return false;
                }
            });
        }
    }

    public void setCustomIcom() {
        //移动到对应的地方，这里先不加

    }

    protected void showLikeButtonWithConfig(boolean show) {
        show = newsDetailStyleConfig.getLinkedHashSet().contains(NewsDetailStyleConfig.LIKE_BUTTON) && show;
        if (show) {
            if (newsLikePresenter == null) {
                likeBadgeView.setArticleItem(articleItem);
                newsLikePresenter = new NewsLikePresenter(this, likeBadgeView);
            }
        }
        showView(show, bottomBarLayout.findViewById(R.id.linear_like_layout));

    }

    protected void showView(boolean show, View view) {
        if (view != null) {
            if (show) {
                view.setVisibility(View.VISIBLE);
            } else {
                view.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void onBackPressed() {
        if (shareWrap.isShowing()) {
            shareWrap.dismiss();
            return;
        }
        super.onBackPressed();
    }


    /**
     * 展示评论相关的View 评论按钮和评论框
     *
     * @param isComment 根据后台的样式配置 true 展示 false 关闭
     *                  当本条新闻禁止评论的时候，即使后台打开了评论框也不显示。
     */
    protected void showCommentRelativeView(boolean isComment) {
        showLetMeSayWithConfig(isComment);
        showCommentButtonWithConfig(isComment);
    }

    protected boolean isShowComment(ArticleItem articleItem) {
        return "1".equals(articleItem.getIsComment());
    }


    @Override
    protected void onResume() {
        super.onResume();
        if (collectionController == null) {
            // bottomCollectionBtn.setEnabled(false);
            collectionController = new CollectionController(this, catalogItem, articleItem, this);
            collectionController.isTencentAct = isTencentAct;
            collectionController.isVrUrl = isVrUrl;
            collectionController.checkCollection();
        }
        getLikeStatus();
        setCommentData();
    }

    public void getLikeStatus() {
        try {
            SupportKit.INSTANCE.initSupportViewState(this, SupportKit.TYPE_1, "" + articleItem.getId(), likeBadgeView);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (newsLikePresenter != null) {
            newsLikePresenter.addLikeDataInvoke.destory();
        }
//        if (commentDialogFram != null) {
//            commentDialogFram.dismiss();
//            commentDialogFram.destory();
//        }
        if (receiver != null) {
            unregisterReceiver(receiver);
            receiver = null;
        }
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    /**
     * 是否显示点赞控件
     *
     * @return
     */
    public boolean showLikeViews() {
        return articleItem != null && articleItem.getTagsflag() == 1 && AppFactoryGlobalConfig.getAppServerConfigInfo(this).getOtherFunction() != null && AppFactoryGlobalConfig.getAppServerConfigInfo(this).getOtherFunction().getSupport() > 0;
    }

    /**
     * 设置点赞数
     *
     * @param type 点赞类型 1文稿，5爆料，6问政。默认为1
     */
    protected void setLikeButtonNumb(int type) {
        if (showLikeViews()) {
            if (newsLikePresenter == null) {
                likeBadgeView.setArticleItem(articleItem);
                newsLikePresenter = new NewsLikePresenter(this, likeBadgeView);
            }
            likeBadgeView.setLikeNum(articleItem);
            NewsLikePresenter.initLikes(newsLikePresenter.addLikeDataInvoke, likeBadgeView, likeBadgeView.getBadgeTextView(), articleItem, this, type, isLive, isPreview, likeClickListener);
        }
    }


    private void showCollectionBtnStatus(boolean collected) {
        Drawable defaultDrawable;
        if (collected) {
            if (isWhiteColor) {
                defaultDrawable = ContextCompat.getDrawable(this, R.drawable.icon_micro_collected);
            } else defaultDrawable = ContextCompat.getDrawable(this, R.drawable.icon_micro_collected);
            if (TextUtils.isEmpty(newsDetailStyleConfig.getShowIcon().collection_button_on_icon))
                bottomCollectionBtn.setImageDrawable(defaultDrawable);
            else
                FunKt.otherLoad(this, newsDetailStyleConfig.getShowIcon().collection_button_on_icon, new RequestListener<Drawable>() {

                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                        if (isFinishing() || isDestroyed()) {
                            return false;
                        }
                        bottomCollectionBtn.setImageResource(R.drawable.icon_micro_collected);
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Drawable drawable, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                        if (isFinishing() || isDestroyed()) {
                            return false;
                        }
                        if (isWhiteColor) {
                            bottomCollectionBtn.setImageDrawable(defaultDrawable);
                        } else {
                            bottomCollectionBtn.setImageDrawable(drawable);
                        }
                        return false;
                    }
                });
        } else {
            if (isWhiteColor) {
//                defaultDrawable = Utility.tintDrawable(Color.parseColor("#FFD8D8D8"), ContextCompat.getDrawable(this, R.drawable.common_un_collect));
                defaultDrawable = ContextCompat.getDrawable(this, R.drawable.icon_micro_uncollect);
            } else defaultDrawable = ContextCompat.getDrawable(this, R.drawable.icon_micro_uncollect);
            if (TextUtils.isEmpty(newsDetailStyleConfig.getShowIcon().collection_button_un_icon))
                bottomCollectionBtn.setImageDrawable(defaultDrawable);
            else
                FunKt.otherLoad(this, newsDetailStyleConfig.getShowIcon().collection_button_un_icon, new RequestListener<Drawable>() {

                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                        if (isFinishing() || isDestroyed()) {
                            return false;
                        }
                        bottomCollectionBtn.setImageResource(R.drawable.icon_micro_uncollect);
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Drawable drawable, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                        if (isFinishing() || isDestroyed()) {
                            return false;
                        }

                        if (isWhiteColor) {
                            bottomCollectionBtn.setImageDrawable(defaultDrawable);
                        } else {
                            bottomCollectionBtn.setImageDrawable(drawable);
                        }
                        return false;
                    }
                });
        }
    }

    @Override
    public void onCollectionStatus(String sourceId, boolean collected) {
        bottomCollectionBtn.setEnabled(true);
        showCollectionBtnStatus(collected);
    }

    @Override
    public void onCollectionOk(String sourceId, boolean collected, String msg) {
        bottomCollectionBtn.setEnabled(true);
        showCollectionBtnStatus(collected);
    }

    @Override
    public void onCollectionErr(String msg) {
        bottomCollectionBtn.setEnabled(true);
    }


    public void refreshBottomStyle(ArticleItem articleItem, int type) {
        this.articleItem = articleItem;
        shareWrap.init(articleItem, catalogItem);
        setLikeButtonNumb(type);
        if (collectionController != null)
          collectionController.articleItem = articleItem;
        setCommentData();
    }

    @Override
    protected void moreClicked() {
        if (!TextUtils.isEmpty(isPreview) && isPreview.equals("1")) {
            ToastUtil.show(this, "预览稿件不支持当前操作");
            return;
        }
        if ("军号环球".equals(articleItem.getTitle()))
            shareWrap.show(mRootView, true, false, false);
        else
            shareWrap.show(mRootView);
    }

    public void setCommentPhotoColor() {
        final Drawable commenteDrawable = ContextCompat.getDrawable(this, R.drawable.badge_view_text_red_shape);
        commentBadgeView.setBadgeTextBgDrawable(commenteDrawable);
        commentBadgeView.setBadgeTextColor(Color.WHITE);
        final Drawable likeDrawable = ContextCompat.getDrawable(this, R.drawable.badge_view_text_red_shape);
        likeBadgeView.setBadgeTextBgDrawable(likeDrawable);
        likeBadgeView.setBadgeTextColor(Color.WHITE);
    }


    /**
     * 设置评论
     */
    protected void setCommentData() {
        if (isShowComment(articleItem)) {
            showCommentRelativeView(true);
            commentBadgeView.setBadgeTextBgColor(getResources().getDimensionPixelOffset(R.dimen.dimen8), Color.RED);
            commentBadgeView.setBadgeText(StringUtils.toCommentTenThousand(String.valueOf(articleItem.getCommentCount())));
            if (articleItem.getCommentCount() > 0) {
                commentBadgeView.showBadgeText(true);
            } else {
                commentBadgeView.showBadgeText(false);
            }

        } else {
            showCommentRelativeView(false);
        }
    }

    /**
     * 设置底部按钮为白色显示，图集需要变白色
     * 着色处理放到glide加载成功监听里面
     */
    public boolean isWhiteColor = false;

    public void setWhiteBottomButton() {
        isWhiteColor = false;
    }


    protected void initNetStatusBroad() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
        intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        receiver = new NetChangeReceiver();
        registerReceiver(receiver, intentFilter);

    }

    protected boolean hasNetWork = false;

    protected class NetChangeReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            checkNetType();
        }
    }

    public boolean isShowNetWorDialog() {
        return true;
    }

    protected void checkNetType() {
        if (isFinishing() || isDestroyed()) {
            return;
        }
        PermissionUtil.INSTANCE.invokeNetWorkPermission(this, new IPermissionsAgree() {
            @Override
            public void onAgree() {
                extracted();

            }

            @Override
            public void onRefuse() {

            }

            @Override
            public void hasPermissions() {
                extracted();

            }
        });
    }

    private void extracted() {
        ConnectivityManager cm = SystemServiceManager.getConnectivityManager(this);
        NetworkInfo networkInfo = cm.getActiveNetworkInfo();
        //有網絡
        if (networkInfo != null && networkInfo.isAvailable()) {
            hasNetWork = true;
        } else {
            //没网络
            hasNetWork = false;
            if (isShowNetWorDialog()) {
                showNetWorDialog();
            }
            noNetwork();
        }
    }

    public void noNetwork() {

    }

    public void showNetWorDialog() {

//        String title = getString(R.string.home_tishi);
//        String name = getString(R.string.network_checktips);
////        String yes = getString(R.string.open_push_ok);
////        String no = getResources().getString(R.string.open_push_no);
//        final Dialog dialog = new Dialog(this, R.style.CustomDatePickerDialog);
//        //2、设置布局
//        View view = View.inflate(this, R.layout.popup_window_view_wangl, null);
//
//        //拿到两个按钮
//        TextView Te_title = view.findViewById(R.id.Te_pop_Title);
//        TextView Te_name = view.findViewById(R.id.Te_pop_name);
//        TextView Te_yes = view.findViewById(R.id.Te_pop_yes);
////        TextView Te_no = view.findViewById(R.id.Te_pop_no);
//        Te_title.setText(title);
//        Te_name.setText(name);
////        Te_yes.setText(yes);
//        Te_yes.setText("确定");
////        Te_no.setText(no);
//        dialog.setContentView(view);
//
//        Te_yes.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//                dialog.dismiss();
//            }
//        });
////        Te_no.setOnClickListener(new View.OnClickListener() {
////            @Override
////            public void onClick(View v) {
////
////                dialog.dismiss();
////            }
////        });
//        dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
//        Window window = dialog.getWindow();
//        //设置弹出位置
//        window.setGravity(Gravity.CENTER);
//        //设置弹出动画
////        window.setWindowAnimations(R.style.dialogstyle);
//        //设置对话 框大小
//        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
//        dialog.show();
////        Utility.dismissAllDialogs();
//
//        Utility.showMessage(this, this.getResources().getString(R.string.home_tishi), this.getResources().getString(R.string.network_checktips), new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
//                Intent intent = null;
//                if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD_MR1) {
//                    intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
//                } else {
//                    intent = new Intent();
//                    ComponentName component = new ComponentName("com.android.settings", "com.android.settings.WirelessSettings");
//                    intent.setComponent(component);
//                    intent.setAction("android.intent.action.VIEW");
//                }
//                startActivity(intent);
//            }
//        }, true);

        //20230310去掉这个无网络提示
//        utility = new Utility();
//        utility.showMessages(this,true);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCollectChange(CollectEvent event) {
        collectionController.checkCollection();
    }

}